<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function getJson(url) {
            return new Promise((resolve, reject) => {
                // 还记得ajax的基本使用吗
                let xhr = new XMLHttpRequest();
                xhr.open('GET', url, true);
                xhr.send(null);

                // 设置状态改变的监听回调函数
                xhr.onreadystatechange = function () {
                    // xhr.readyState == 4表示请求已经完成
                    if (xhr.readyState !== 4) return;

                    // 当请求成功或者失败，需要改变promise实例的状态
                    if (xhr.status >= 200 && xhr.status < 300) {
                        resolve(xhr.response);
                    }
                    else {
                        reject(this.statusText);
                    }
                }
            })
        };

        (async function getData() {
            let data = await getJson('./data.json');
            console.log(data);
        })()
    </script>
</body>

</html>